package com.netease.yanxuan.qa.yx.yuheng.service.impl;


import com.netease.yanxuan.qa.yx.yuheng.dto.TestCaseInfoDto;
import com.netease.yanxuan.qa.yx.yuheng.entity.TestCaseInfo;
import com.netease.yanxuan.qa.yx.yuheng.entity.TestCaseInfoExample;
import com.netease.yanxuan.qa.yx.yuheng.exception.CusYxYuhengException;
import com.netease.yanxuan.qa.yx.yuheng.mapper.TestCaseInfoMapper;
import com.netease.yanxuan.qa.yx.yuheng.service.TestCaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.netease.yanxuan.qa.yx.yuheng.config.YxYuhengConstants.ERROR_CODE;

/**
 * <Description>
 *
 * @author yanlu02
 * @date 2021/1/14 20:07
 */
@Slf4j
@Service
public class TestCaseInfoServiceImpl implements TestCaseInfoService {

    @Autowired
    private TestCaseInfoMapper testCaseInfoMapper;

    @Override
    public List<TestCaseInfo> queryTestCases(String casePath, byte priority, byte status) {
        TestCaseInfoExample example = new TestCaseInfoExample();
        TestCaseInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(casePath)){
            criteria.andCasePathLike("%" + casePath + "%");
        }
        if (priority != -1) {
            criteria.andPriorityEqualTo(priority);
        }
        if (status != -1){
            criteria.andStatusEqualTo(priority);
        }
        return testCaseInfoMapper.selectByExample(example);
    }

    @Override
    public boolean updateTestCase(TestCaseInfoDto testCaseInfoDto) {
        if (testCaseInfoDto.getId() == null) {
            throw new CusYxYuhengException(ERROR_CODE, "参数异常");
        }
        TestCaseInfo testCaseInfo = testCaseInfoMapper.selectByPrimaryKey(testCaseInfoDto.getId());
        if (testCaseInfo == null) {
            throw new CusYxYuhengException(ERROR_CODE, "case不存在");
        }
        BeanUtils.copyProperties(testCaseInfoDto, testCaseInfo);
        int reselt = testCaseInfoMapper.updateByPrimaryKeySelective(testCaseInfo);
        return reselt == 1 ? true : false;
    }
}
